﻿namespace Gaiaware.Web.SkinSandbox
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using Gaia.WebWidgets;
    using Gaia.WebWidgets.CommunityExtensions;
    using Gaia.WebWidgets.Extensions;
    using Utilities;

    public partial class Default : SkinSandboxPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btn_Click(object sender, EventArgs e)
        {
            lblBtn.Text = "Button clicked " + DateTime.Now;
        }

        protected void rbtn_CheckedChanged(object sender, EventArgs e)
        {
            lblRbtn.Text = rbtn.Checked.ToString();
        }

        protected void imgBtn_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            lblImgBtn.Text = "Image button clicked " + DateTime.Now;
        }

        protected void cbx_CheckedChanged(object sender, EventArgs e)
        {
            lblCheckBox.Text = cbx.Checked.ToString();
        }

        protected void lnkBtn_Click(object sender, EventArgs e)
        {
            lblLnkBtn.Text = "LinkButton clicked " + DateTime.Now;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            lblInsideTabView1.Text = "Button triggered inside tabview1";
        }
        protected void autocompleter_GetAutoCompleterItems(object sender, EventArgs e)
        {
            var controls = new List<Control>();
            for (var i = 0; i < 10; i++)
                controls.Add(new Label { Text = "Select " + autocompleter.Text + " " + i });

            foreach (var control in controls)
                autocompleter.AutoCompleterItems.Add(control);
        }
        protected void autocompleter_SelectionChanged(object sender, Gaia.WebWidgets.Extensions.AutoCompleter.AutoCompleterSelectionChangedEventArgs e)
        {
            lblAutocompleter.Text = "You selected: " + ((Label)e.SelectedValue).Text;
        }
        protected void cal_SelectedDateChanged(object sender, EventArgs e)
        {
            lblCalendar.Text = cal.SelectedDate.ToString();
        }
        protected void cal_DayRender(object sender, Gaia.WebWidgets.Extensions.Calendar.DayRenderEventArgs e)
        {
            if (e.CalendarDay.Date == DateTime.Now.AddDays(1).Date
                || e.CalendarDay == DateTime.Now.AddDays(2).Date
                || e.CalendarDay == DateTime.Now.AddDays(3).Date)
                e.DayCssClass = "highlightdate";
        }
        protected void cal_CalendarDayClicking(object sender, Gaia.WebWidgets.Extensions.Calendar.CalendarDayClickingEventArgs e)
        {
            if (e.ClickedDate.Date == DateTime.Now.AddDays(1).Date
               || e.ClickedDate == DateTime.Now.AddDays(2).Date
               || e.ClickedDate == DateTime.Now.AddDays(3).Date)
            {
                e.AllowSelection = false;
                lblCalendar.Text = "You are not allowed to select " + e.ClickedDate.Date;
            }
        }
        protected void extBtnOpenWindow_Click(object sender, EventArgs e)
        {
            wnd.Visible = !wnd.Visible;
        }
        protected void grid_Init(object sender, EventArgs e)
        {
            grid.DataSource = GetDataItems();
            grid.DataBind();
        }

        static IEnumerable<MyEntity> GetDataItems()
        {
            for (var i = 0; i < 10; i++)
                yield return new MyEntity(i, "Title " + i);
        }

        protected void lbx_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblLbx.Text = lbx.SelectedValue;
        }

        protected void timer_OnTick(object sender, EventArgs e)
        {
            lblTimer.Text = "Timer tick at " + DateTime.Now;
        }

        protected void btnToggleTimer_Click(object sender, EventArgs e)
        {
            timer.Enabled = !timer.Enabled;
        }

        protected void itemCommand(object sender, EventArgs e)
        {
            LinkButton linkButton = sender as LinkButton;
            if (linkButton == null) return;
            lblToolbar.Text = "You selected " + linkButton.Text;
        }

        protected void extendedButton_Click(object sender, EventArgs e)
        {
            lblExtendedButton.Text = "Extended Button clicked " + DateTime.Now;
        }

        protected void slider_OnValueChanged(object sender, EventArgs e)
        {
            lblSlider.Text = "New slider value: " + ((Slider)sender).Value;
        }

        protected void inplaceedit_OnTextChanged(object sender, EventArgs e)
        {
            lblInplaceedit.Text = "InPlaceEdit changed to: " + ((InPlaceEdit)sender).Text;
        }

        protected void speedDetector_SpeedDetectionComplete(object sender, SpeedDetector.DetectionCompleteEventArgs e)
        {
            lblSpeedDetector.Text = string.Format(
                 "Your bandwith is meassured to be " +
                 "approximately {0} kbps", GetKpbs(e.TimeSpent));

            btnSpeedDetector.Enabled = true;
        }

        protected void btnSpeedDetector_Click(object sender, EventArgs e)
        {
            btnSpeedDetector.Enabled = false;

            lblSpeedDetector.Text = string.Format(
                "Please wait while downloading {0} MB of gibberish",
                speedDetector.DownloadSize / 1024 / 1024);

            speedDetector.Start();
        }

        private double GetKpbs(TimeSpan timeSpent)
        {
            double fileSize = speedDetector.DownloadSize;
            double rate = fileSize / timeSpent.TotalSeconds;
            double kbps = (rate * 8) / 1024;
            return kbps;
        }


    }

    public class MyEntity
    {
        public MyEntity(int id, string title)
        {
            Id = id;
            Title = title;
        }

        public int Id { get; set; }
        public string Title { get; set; }
    }
}